package com.dgut.demo.service.impl;

import com.dgut.demo.bean.User;
import com.dgut.demo.bean.VipMessage;
import com.dgut.demo.controller.LogsController;
import com.dgut.demo.mapper.VipMapper;
import com.dgut.demo.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther 虎
 * @Data 2021/2/4 23:26
 * @Version 1.0
 */
@Service
public class VipServiceImpl implements VipService {
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private User user;
    @Autowired
    private LogsController logsController;

    @Override
    public List<VipMessage> getAllVipMessage() {
        return vipMapper.getAllVipMessage();
    }

    @Override
    public int addOneVipMessage(VipMessage vipMessage) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        int random = (int)(Math.random()*10);
        String vid='1'+ dateFormat.format(date) + random;
        vipMessage.setVid(vid);
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        vipMessage.setVtime(java.sql.Date.valueOf(dateFormat2.format(date)));
        int flag;
        flag = vipMapper.addOneVipMessage(vipMessage);
        if(flag == 1){
            //log
            String actions = "新增会员【"+vipMapper.getVipMessageById(vid).toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteOneVipMessage(String vid) {
        VipMessage vipMessage = vipMapper.getVipMessageById(vid).get(0);
        //log
        String actions = "删除会员【"+vipMessage.toString()+"】";
        String userId = user.getUserId();
        logsController.addOneLogsMessage(actions,userId);

        return vipMapper.deleteOneVipMessage(vid);
    }

    @Override
    public int updateOneVipMessage(VipMessage vipMessage) {
        VipMessage vipMessages1 = vipMapper.getVipMessageById(vipMessage.getVid()).get(0);
        int flag = vipMapper.updateOneVipMessage(vipMessage);
        if(flag == 1){
            VipMessage vipMessage2 = vipMapper.getVipMessageById(vipMessage.getVid()).get(0);
            //log
            String actions = "更新会员信息【"+vipMessages1.toString()+"】--》【"+vipMessage2.toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
        }
        return flag;
    }

    @Override
    public List<VipMessage> getVipMessageById(String vid) {
        return vipMapper.getVipMessageById(vid);
    }

    @Override
    public List<VipMessage> getVipMessageByName(String vname) {
        return vipMapper.getVipMessageByName(vname);
    }

    @Override
    public List<VipMessage> getVipMessageByPhone(String vphone) {
        return vipMapper.getVipMessageByPhone(vphone);
    }

    @Override
    public List<VipMessage> getVipMessageByAddress(String vaddress) {
        return vipMapper.getVipMessageByAddress(vaddress);
    }
}
